/**------------------------------------------------------------
 *
 * Copyright © 2014 rubbervalley. All rights reserved.
 *
 * @Title: PlatStoreCategoryController.java
 * @Package: com.rubbervalley.uyo.actions
 * @Description: 公司类别
 * @author: zhoulei
 * @date: 上午10:35:28
 * @version: V1.0
 * 
 * Update History:
 * 
 ------------------------------------------------------------*/
package com.rubbervalley.uyo.actions.administrator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.rubbervalley.framework.core.common.MessageUtil;
import com.rubbervalley.framework.core.common.ObjectMessages;
import com.rubbervalley.platform.business.PlatStoreCategoryService;
import com.rubbervalley.platform.business.PlatStoreService;
import com.rubbervalley.platform.domain.PlatStore;
import com.rubbervalley.platform.domain.PlatStoreCategory;

@Controller
@RequestMapping(value = "/uyo")
public class PlatStoreCategoryController {
	@Autowired
	private PlatStoreCategoryService platStoreCategoryService;
	@Autowired
	private PlatStoreService platStoreService;
	@Autowired
	private MessageUtil messageUtil;

	private static final Logger logger = LoggerFactory
			.getLogger(PlatStoreCategoryController.class);

	@RequestMapping(value = "/platStoreCategorys", method = RequestMethod.GET)
	String platStoreCategorys(Map<String, Object> model)
	{
		logger.debug("PlatStoreCategoryController");
		return "/uyo/administrator/platStoreCategory/platStoreCategorys.ftl";
	}

	@RequestMapping(value = "/platStoreCategory/{id}", method = RequestMethod.GET)
	public String detail(Map<String, Object> model,
			@PathVariable("id") Integer id)
	{
		// TODO: 初始化
		PlatStoreCategory bean = platStoreCategoryService.select(id);
		model.put("bean", bean);
		return "/uyo/administrator/platStoreCategory/detail.ftl";
	}

	public void validate(PlatStoreCategory platStoreCategory, Errors errors)
	{
		Integer id = platStoreCategory.getId();
		PlatStoreCategory sp2 = platStoreCategoryService.selectCategory(platStoreCategory);
		// 更新如果有code相同，则排除
		if( sp2!=null && id!=null   && !(sp2.getId().equals(id))){
			errors.reject("uyo.codeexit.error");
		}
		else if( sp2!=null  && id==null ){ // 增加如果存在已有code，排除
			errors.reject("uyo.codeexit.error");
		}
	}

	@RequestMapping(value = "/platStoreCategory/add", method = RequestMethod.GET)
	public String add(Map<String, Object> model
		,@RequestParam(value="parentId" , required=false) Integer parentId
		,@RequestParam(value="parentName" , required=false) Integer parentName)
	{
		if(parentId!=null && parentId!=0){
			PlatStoreCategory parent = platStoreCategoryService.select(parentId);
			model.put("parent", parent);
		}
		return "/uyo/administrator/platStoreCategory/add.ftl";
	}

	@RequestMapping(value = "/platStoreCategory/add", method = RequestMethod.POST)
	@ResponseBody
	public ObjectMessages add(Map<String, Object> model,
			PlatStoreCategory platStoreCategory, BindingResult result)
	{
		// TODO:修改代码
//		validate(platStoreCategory, result); // 数据校验
//		if (result.hasErrors()) {
//			// TODO:校验失败
//			return messageUtil.messages("common.add.error");
//		} else {
			this.platStoreCategoryService.insert(platStoreCategory);
			// TODO:成功处理
			return messageUtil.messages("common.add.success");
//		}

	}

	@RequestMapping(value = "/platStoreCategory/{id}/update", method = RequestMethod.GET)
	public String update(Map<String, Object> model,
			@PathVariable("id") Integer id)
	{
		PlatStoreCategory bean = platStoreCategoryService.select(id);
		model.put("bean", bean);
		PlatStoreCategory parent = platStoreCategoryService.select(bean.getParentId());
		model.put("parent", parent);
		return "/uyo/administrator/platStoreCategory/update.ftl";
	}

	@RequestMapping(value = "/platStoreCategory/{id}/update", method = RequestMethod.POST)
	@ResponseBody
	public ObjectMessages update(Map<String, Object> model,
			PlatStoreCategory platStoreCategory, BindingResult result)
	{
		// TODO:修改代码
		validate(platStoreCategory, result); // 数据校验
		if (result.hasErrors()) {
			// TODO:校验失败
			return messageUtil.messages("common.update.error");
		} else {
			this.platStoreCategoryService.update(platStoreCategory);
			// TODO:成功处理
			return messageUtil.messages("common.update.success");
		}

	}

	@RequestMapping(value = "/platStoreCategory/{id}/delete")
	@ResponseBody
	public ObjectMessages delete(Map<String, Object> model,
			@PathVariable("id") Integer id)
	{
		//若该类下有子类或者有公司属于该行业 则不能删除
		List<PlatStoreCategory> child=platStoreCategoryService.selectByParentId(id);
	    List<PlatStore> stores=platStoreService.selectByCategoryId(id);
	    if(child.size()>0 || stores.size()>0){
	    	return messageUtil.messages("common.delete.error");
	    }
	    PlatStoreCategory bean = new PlatStoreCategory();
	    bean.setId(id);
	    bean.setDelflag((byte) 1);
		this.platStoreCategoryService.update(bean);
		return messageUtil.messages("common.delete.success");
	}
	
	/**
	 * ajax子资源
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/platStoreCategory/childDatas")
	public @ResponseBody java.util.List<HashMap<String,Object>> ChildDatas(
			@RequestParam(value="id",required=false) Integer id)
	{
		if(id ==null){
			id=0;
		}
		java.util.List<PlatStoreCategory> categories = this.platStoreCategoryService.selectChildDatas(id);
		java.util.List<HashMap<String,Object>>  lst = new java.util.ArrayList<HashMap<String,Object>>();
		if(categories!=null)
		{
			for(PlatStoreCategory category:categories)
			{
				HashMap<String,Object> ztreeNode= new HashMap<String,Object>();
				ztreeNode.put("id", category.getId());
				ztreeNode.put("name", category.getName());
				ztreeNode.put("code", category.getCode());
				if(category.getChildDatas() !=null && category.getChildDatas().size()>0)
				{
					ztreeNode.put("isParent", new Boolean(true));
				}else{
					ztreeNode.put("isParent", new Boolean(false));
				}
				
				lst.add(ztreeNode);
			}
		}
		return lst;
	}
	/**
	 * 行业树形图
	 * @param model
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping(value = "/platStoreCategory/dataTree", method = RequestMethod.GET)
	public String toTreePage(Map<String, Object> model
			,@RequestParam(value="selectedId" , required=false) Integer selectedId
			) throws JsonProcessingException {
		model.put("selectedId", selectedId);
		return "/uyo/administrator/platStoreCategory/dataTree.ftl";
	}
}